/**
 * reducer 函数签名是 : (state, action) => newState
 * 学习目标：redux - action 的学习
 * 描述“发生了什么”的 action 对象
 * this.$emit('add',1)  ---  vuex 发生了 add 的事情，传递参数1
 * action：
 * 1. 必须要有 type 属性，描述发生了什么
 * 2. 可以传递 payload(不要起其他的名字) ，作为传递的参数
 *
 */
import { legacy_createStore as createStore } from 'redux';

const store = createStore((state = { count: 0 }, action) => {
  console.log('action -----> ', action);
  if (action.type === 'add') {
    return { ...state, count: state.count + action.payload };
  }
  if (action.type === 'dec') {
    return { ...state, count: state.count - action.payload };
  }
  return state;
});

console.log('store -----> ', store);
console.log('store.getState() -----> ', store.getState().count);

store.dispatch({ type: 'add', payload: 1 });
store.dispatch({ type: 'add', payload: 10 });
store.dispatch({ type: 'dec', payload: 1 });
store.dispatch({ type: 'dec', payload: 10 });
// store.dispatch({ type: 'dec' });
console.log('store.getState() -----> ', store.getState().count);

export default function App(params) {
  return <></>;
}
